package com.sdcqjy.mylibrary;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @ClassName: FileOperateUtil
 * @Description: 文件操作工具类
 */
public class FileOperateUtil {
    public final static int ROOT = 0;// 根目录
    public final static int TYPE_IMAGE = 1;// 图片
    public final static int TYPE_THUMBNAIL = 2;// 缩略图
    public final static int TYPE_VIDEO = 3;// 视频
    public final static int TYPE_TEXT = 4;// 文本
    public final static int TYPE_ERR = 5;// 错误日志
    public final static int TYPE_Cache = 6;// 缓存
    public final static int TYPE_NetRes = 7;// 网络资源
    public final static int TYPE_Sound = 8;// 声音
    public final static int TYPE_Tmp = 9;// 临时文件
    public final static int GlideCache = 10;//GlideCache

    public static FileOperateUtil fileOperateUtil;
    public String dirName = "";
    public String dirName2 = "";

    public static FileOperateUtil getInstance() {
        if (fileOperateUtil == null) {
            synchronized (FileOperateUtil.class) {
                if (fileOperateUtil == null) {
                    fileOperateUtil = new FileOperateUtil();
                }
            }
        }
        return fileOperateUtil;
    }

    public static void init(Context context, String dirName) {
        FileOperateUtil operateUtil = getInstance();
        operateUtil.dirName2 = context.getPackageName();
        operateUtil.dirName = dirName;
    }

    //        folderPath = FileOperateUtil.getSDCard() + File.separator + "Android" + File.separator + "data" + File.separator + getPackageName();
//    public String getAppDir() {
//        return getSDCard() + File.separator + "Android" + File.separator + "data" + File.separator + dirName2;
//    }

    public String getAppDir() {
        return getSDCard() + File.separator + getDirName() + File.separator;
    }

    public String getDirName() {
        if (TextUtils.isEmpty(dirName)) dirName = "temp";
        return dirName;
    }

    public static String getSDCard() {
        return Environment.getExternalStorageDirectory().getPath();
    }

    /**
     * 获取文件夹路径
     *
     * @param type 文件夹类别
     */
    public String getFolderPath(int type) {
        // 本业务文件主目录
        StringBuilder pathBuilder = new StringBuilder();
        // 添加应用存储路径
        pathBuilder.append(getAppDir());
        // 添加当然文件类别的路径
        switch (type) {
            case TYPE_IMAGE:
                pathBuilder.append("Image");
                pathBuilder.append(File.separator);
                break;
            case TYPE_VIDEO:
                pathBuilder.append("Video");
                pathBuilder.append(File.separator);
                break;
            case TYPE_TEXT:
                pathBuilder.append("Text");
                pathBuilder.append(File.separator);
                break;
            case TYPE_ERR:
                pathBuilder.append("Err");
                pathBuilder.append(File.separator);
                break;
            case TYPE_Cache:
                pathBuilder.append("Cache");
                pathBuilder.append(File.separator);
                break;
            case TYPE_NetRes:
                pathBuilder.append("NetRes");
                pathBuilder.append(File.separator);
                break;
            case TYPE_Sound:
                pathBuilder.append("Sound");
                pathBuilder.append(File.separator);
                break;
            case TYPE_THUMBNAIL:
                pathBuilder.append("Thumbnail");
                pathBuilder.append(File.separator);
                break;
            case TYPE_Tmp:
                pathBuilder.append("Temp");
                pathBuilder.append(File.separator);
                break;
            case GlideCache:
                pathBuilder.append("GlideCache");
                pathBuilder.append(File.separator);
                break;
            default:
                break;
        }
        String folderPath = pathBuilder.toString();
        File folder = new File(folderPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return folderPath;
    }

    /**
     * 默认删除569下的文件
     */
    public void deleteDirFile() {
        deleteDirFile(5);
        deleteDirFile(6);
        deleteDirFile(9);
        deleteDirFile(10);
    }

    /**
     * 删除文件夹下的所有文件
     */
    public void deleteDirFile(int type) {
        String folderPath = getFolderPath(type);
        File file = new File(folderPath);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files)
                file1.delete();
        }
    }

    public long getDataSize() {
        long total = getDataSize(5);
        total += getDataSize(6);
        total += getDataSize(9);
        total += getDataSize(10);
        return total;
    }

    public long getDataSize(int type) {
        long total = 0;
        String folderPath = getFolderPath(type);
        File file = new File(folderPath);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files)
                total += file1.length();
            //file1.delete();
        }
        return total;
    }

    /**
     * 向文本文件写入数据<br>
     * 文件不存在新建
     *
     * @return 保存成功或失败
     */
    public static boolean textWrite(String fileName, String msg) {
        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            fos.write(msg.getBytes());
            fos.flush();
            fos.close();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 打开内部储存位置
     */
    public static String getPrivatePath(Context context) {
        return getPrivateDir(context).getAbsolutePath();
    }

    public static File getPrivateDir(Context context) {
        //FileOutputStream fileOutputStream = context.openFileOutput(fileName, 0);
        return context.getFilesDir();
    }

    public static void makeDirs(String file) {
        File fs = new File(file);
        if (!fs.exists())
            fs.mkdir();
    }

    public static String getFileNameOfUrl(String url) {
        String[] split = url.split("#");
        if (split.length > 1) url = split[0];
        split = url.split("\\?");
        if (split.length > 1) url = split[1];
        split = url.split("/");
        return split[split.length - 1];
    }
}